<?php
// $Id $
/**
*Provide API functions for user_location module to use, or developers can reuse in their other drupal projects.
*/

/**
The function user_location which can retrieve user's geographic information with self-defined function ip2geo below.
*/

function user_location($uid) {
//Query user's ip information from sessions table first.
$db_query_handle = db_query ('SELECT hostname FROM {sessions} WHERE uid = %d ORDER BY timestamp DESC LIMIT 1', $uid );
 if (db_num_rows($db_query_handle) === 0) {
     //If no record in sessions table, query from user_location table which we set up for this module.
     $db_query_handle = db_query ('SELECT user_ip FROM {user_location} WHERE uid = %d', $uid );
	 if (db_num_rows($db_query_handle) === 0) {
	    //If no record in both sessions and user_location tables, assign a defaut value.
	     $user_location = null;
		    
		 return $user_location;
		 exit;
		}
	}
     $ip = db_result($db_query_handle);
	 $user_location = ip2geo ($ip);
  return $user_location;
} 

/**
The function which can help you find geographic information through ip address, including country city latitude and longitude.
*/
function ip2geo ($ip) {
    include_once("geoipcity.inc");
    include_once("geoipregionvars.php");
    $gi = geoip_open("modules\user_location\ipdata\GeoLiteCity.dat", GEOIP_MEMORY_CACHE);
    $record = geoip_record_by_addr($gi, $ip);
    $geo_info = array(
        'country_code' => $record->country_code,
	    'country_name' => $record->country_name,
        'region_code' => $record->region,
	    'region_name' => $GEOIP_REGION_NAME[$record->country_code][$record->region],
        'city' => $record->city,
        'post_code' => $record->post_code,
        'latitude' => $record->latitude,
        'longitude' => $record->longitude,
        'dma_code' => $record->dma_code,
        'area_code' => $record->area_code
    );   
    geoip_close($gi);
  return $geo_info;
}

/**
* The function which can specify the client's ip address even when using  a transparent proxy, quoted from drupal 6.
*/
function ip_address() { 
  static $ip_address = NULL; 

  if (!isset($ip_address)) { 
    $ip_address = $_SERVER['REMOTE_ADDR']; 
    if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) { 
      // If there are several arguments, the leftmost one is the farthest client 
      list($ip_address, ) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); 
    } 
  } 

  return $ip_address; 
} 

/**
*Generate a  geographic map of png picture through yahoo map api
*/
function geo_map($appid, $latitude, $longitude, $image_height, $image_width, $zoom) {
    //Built a rest url to query from yahoo map api.
    $url = "http://local.yahooapis.com/MapsService/V1/mapImage?appid=" .$appid;
    $url.= "&latitude=" .$latitude;
    $url.= "&longitude=" .$longitude;
    $url.= "&image_height=" .$image_height;
    $url.= "&image_width=" .$image_width;
    $url.= "&zoom=" .$zoom;
    $url.= "&output=php";
	// for many hosting service disable allow_url_fopen or php safety mode is turned on, we 'd rather use curl instead of fopen to get remote file.
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    if ($response = curl_exec($ch)) {
        $phpobj = unserialize($response);
        $pic_url = $phpobj['Result'];
        $geo_map = '<img src="' .$pic_url .'" />';
	} else {
	    $geo_map = '<b>Error</b> : the map cannot be generated at this time (maybe due to the internet connection), try again.';
		}
    return $geo_map;
}
